import React from 'react';
import Login from 'login'
import Home from 'page/HomePage'
import {
    BrowserRouter as Router,
    Route,
    Redirect
} from 'react-router-dom'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const PrivateRoute = ({component: Component, ...props}) => {
    // 解构赋值 将 props 里面的 component 赋值给 Component
    return <Route {...props} render={(p) => {
        //const login = document.cookie.includes('isLogin=true')
        const login = true
        if (login){ // 如果登录了, 返回正确的路由
            return <Component />
        } else { // 没有登录就重定向至登录页面
            return <Redirect to={{
                pathname: '/',
                state: {
                    from: p.location.pathname
                }
            }}/>
        }
    }}/>
}
class Main extends React.Component{
    componentDidMount(){
        NProgress.done()
    }
    componentWillMount () {
        NProgress.start()
    }
    
    componentDidUpdate () {
        
    }
    render(){
        return(
            <Router>
                <div style={{height:'100%'}}>
                    {/* exact 准确的，精准的，防止所有/...接口被调用 */}
                    <Route path="/" exact component={Login}></Route>
                    <PrivateRoute path="/index" component={Home}/>
                </div>
            </Router>
        )
    }
}

export default Main;
